package com.book.controller.reader;

import com.alibaba.excel.EasyExcel;
import com.book.core.domain.Books;
import com.book.core.domain.ReaderTypes;
import com.book.core.mode.R;
import com.book.listener.GenericExcelDataListener;
import com.book.service.ReaderTypesService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/reader/type")
@RequiredArgsConstructor
public class ReaderTypeController {
    private final ReaderTypesService readerTypesService;

    @GetMapping("/list")
    public R<List<ReaderTypes>> getReaderTypes() {
        return R.ok(readerTypesService.list());
    }
    @PostMapping
    public R<String> saveReaderTypes(@RequestBody @Validated ReaderTypes readerTypes) {
        readerTypesService.save(readerTypes);
        return R.ok("新增成功");
    }
    @PutMapping
    public R<String> updateReaderTypes(@RequestBody @Validated ReaderTypes readerTypes) {
        readerTypesService.updateById(readerTypes);
        return R.ok("修改成功");
    }
    @DeleteMapping("/{id}")
    public R<String> deleteReaderTypes(@PathVariable Integer id) {
        readerTypesService.removeById(id);
        return R.ok("删除成功");
    }
    @PostMapping("/import")
    public R<String> importBookExcel(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ReaderTypes.class, new GenericExcelDataListener<ReaderTypes>(readerTypesService)).sheet().doRead();
        return R.ok("导入成功",null);
    }
}
